using System;
using System.Xml;
using DesignByContract;
using Outcoding.GraphGenerator.Entities;
using Outcoding.GraphGenerator.Util.Logging;

namespace Outcoding.GraphGenerator.Composition{
    public class LabelBuilder : BuilderBase{
        #region Attributes

        private string fontColorAttr = "font-color";
        private string fontDecorationAttr = "font-decoration";
        private string fontNameAttr = "font-name";
        private string fontSizeAttr = "font-size";
        private string hAlignAttr = "halign";
        private string visibleAttr = "visible";

        #endregion

        private readonly string mNodeName = XmlTagNames.label.ToString();

        private XmlNode mNode;

        private string mNodePath{
            get { return "./" + mNodeName; }
        }

        public XmlNode Node{
            get { return mNode; }
            set { mNode = value; }
        }

        public override void SelectNode(XmlDocument document){
            //HACK: this is done in order to work with labelcollection
        }

        public override void Compose(){
            try{
                BuildObject(mNode);
            }
            catch (PreconditionException preExc){
                LogManager.Instance.LogWarning("Label entity could not be created; process aborted.", preExc);
            }
        }

        public override void Compose(XmlNode node){
            mNode = node.SelectSingleNode(mNodePath);
            try{
                BuildObject(mNode);
            }
            catch (PreconditionException preExc){
                LogManager.Instance.LogWarning("Label entity could not be created; process aborted.", preExc);
            }
        }

        public override void AppendToParent(object parent){
            if (parent is Shape)
                (parent as Shape).Content = (Label) mProduct;
            else if (parent is Graph)
                (parent as Graph).Title = (Label) mProduct;
            else if (parent is ValueAxis)
                (parent as ValueAxis).Title = (Label) mProduct;
            else if (parent is CategoryAxis)
                (parent as CategoryAxis).Title = (Label) mProduct;
            else if (parent is SeriesAxis)
                (parent as SeriesAxis).Title = (Label) mProduct;
            else if (parent is Series)
                (parent as Series).Title = (Label) mProduct;
            else if (parent is Point)
                (parent as Point).Label = (Label) mProduct;
        }

        private void BuildObject(XmlNode labelXmlNode){
            var label = new Label();
            try{
                if (labelXmlNode.Attributes[fontNameAttr] != null)
                    label.FontName = ParseFontNameAttribute(labelXmlNode);
                if (labelXmlNode.Attributes[fontSizeAttr] != null)
                    label.FontSize = ParseFontSizeAttribute(labelXmlNode);
                if (labelXmlNode.Attributes[fontDecorationAttr] != null){
                    label.IsFontBold = ParseIsFontBoldAttribute(labelXmlNode);
                    label.IsFontItalic = ParseIsFontItalicAttribute(labelXmlNode);
                    label.IsFontUnderline = ParseIsFontUnderlineAttribute(labelXmlNode);
                }
                if (labelXmlNode.Attributes[fontColorAttr] != null)
                    label.Color = ParseColorAttribute(labelXmlNode);
                if (labelXmlNode.Attributes[hAlignAttr] != null)
                    label.HorizontalAlignment = ParseHorizontalAlignmentAttribute(labelXmlNode);
                if (labelXmlNode.Attributes[visibleAttr] != null)
                    label.IsVisible = ParseIsVisibleAttribute(labelXmlNode);
                label.Text = ParseTextValue(labelXmlNode);
            }
            catch (PreconditionException preExc){
                //log error
                label = null;
                throw;
            }
            mProduct = label;
        }

        private string ParseFontNameAttribute(XmlNode labelXmlNode){
            Check.Require(labelXmlNode.Attributes[fontNameAttr] != null, "font-name attribute not present");
            return labelXmlNode.Attributes[fontNameAttr].Value;
        }

        private int ParseFontSizeAttribute(XmlNode labelXmlNode){
            Check.Require(labelXmlNode.Attributes[fontSizeAttr] != null, "font-size attribute not present");
            return Int32.Parse(labelXmlNode.Attributes[fontSizeAttr].Value);
        }

        private bool ParseIsFontBoldAttribute(XmlNode labelXmlNode){
            Check.Require(labelXmlNode.Attributes[fontDecorationAttr] != null, "font-decoration attribute not present");
            return labelXmlNode.Attributes[fontDecorationAttr].Value.Contains("bold");
        }

        private bool ParseIsFontItalicAttribute(XmlNode labelXmlNode){
            Check.Require(labelXmlNode.Attributes[fontDecorationAttr] != null, "font-decoration attribute not present");
            return labelXmlNode.Attributes[fontDecorationAttr].Value.Contains("italic");
        }

        private bool ParseIsFontUnderlineAttribute(XmlNode labelXmlNode){
            Check.Require(labelXmlNode.Attributes[fontDecorationAttr] != null, "font-decoration attribute not present");
            return labelXmlNode.Attributes[fontDecorationAttr].Value.Contains("underline");
        }

        private PPAlignment ParseHorizontalAlignmentAttribute(XmlNode labelXmlNode){
            Check.Require(labelXmlNode.Attributes[hAlignAttr] != null, "horizontal alignment attribute not present");
            return ConvertHorizontalAlignFromName(labelXmlNode.Attributes[hAlignAttr].Value);
        }

        private bool ParseIsVisibleAttribute(XmlNode labelXmlNode){
            Check.Require(labelXmlNode.Attributes[visibleAttr] != null, "visible attribute not present");
            return Boolean.Parse(labelXmlNode.Attributes[visibleAttr].Value);
        }

        private string ParseTextValue(XmlNode labelXmlNode){
            return labelXmlNode.InnerText.Trim();
        }

        private long ParseColorAttribute(XmlNode labelXmlNode){
            Check.Require(labelXmlNode.Attributes[fontColorAttr] != null, "color attribute not present");

            return Entities.Util.ColorTranslation(labelXmlNode.Attributes[fontColorAttr].Value);
        }

        private PPAlignment ConvertHorizontalAlignFromName(string value){
            switch (value){
                case "ppAlignCenter":
                    return PPAlignment.ppAlignCenter;
                case "ppAlignDistribute":
                    return PPAlignment.ppAlignDistribute;
                case "ppAlignJustify":
                    return PPAlignment.ppAlignJustify;
                case "ppAlignLeft":
                    return PPAlignment.ppAlignLeft;
                case "ppAlignRight":
                    return PPAlignment.ppAlignRight;
            }
            return PPAlignment.ppAlignUnknown;
        }
    }
}